<?php
namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Mood extends Base
{
    protected $table = "mood";
    protected $redis_mood  = 'MOOD:LIST';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 获取心情
     * @return array
     */
    public function getAll($map=0){
        $list = Redis::get($this->redis_mood);
        if(empty($list)){
            $list = DB::table($this->table)
                ->select("id","name","image")
                ->where(['is_deleted' => 0])
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($this->redis_mood, 3600, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }
        foreach($list as $key=>$val) {
            $list[$key]['image'] = build_url($val['image']);
        }
        if($map) {
            $list = array_column($list, null, "id");
        }

        return $list;
    }

}
